<?php

$file_id    = $path[1];
$cache_file = $app_root . $site['home_dir'] . "/cache/files/{$file_id}";

if (ctype_digit($file_id)) {
	
	$file = $db->fetchRow("SELECT * FROM cms_files WHERE id=? AND site_id=?", array($file_id, $site['id']));
	
	if (!file_exists($cache_file)) {
		mkdir(dirname($cache_file), 0755, true);
		file_put_contents($cache_file, $file['data']);
	}
	header("Content-type: {$file['mime_type']}");
	header("Content-disposition: inline; filename={$file['filename']}");
	readfile($cache_file);
	exit;
	
} else {
	require '404.php';
}
